package org.smartboot.flow.core.builder;


import org.smartboot.flow.core.attribute.AttributeValueResolver;
import org.smartboot.flow.core.attribute.Attributes;
import org.smartboot.flow.core.component.Component;
import org.smartboot.flow.core.util.AssertUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author qinluo
 * @date 2022-11-13 17:49:01
 * @since 1.0.0
 */
public abstract class AbstractComponentBuilder<T, S> {

    private final AttributeValueResolver valueResolver = AttributeValueResolver.getInstance();

    /**
     * Component attribute attributes.
     */
    private final Map<Attributes, Object> settings = new HashMap<>(8);


    public AbstractComponentBuilder<T, S> apply(Attributes attributes, Object value) {
        AssertUtil.notNull(attributes, "Unknown attribute");
        AssertUtil.notNull(value, "null");

        Object resolved = valueResolver.resolve(attributes, value);

        AssertUtil.isTrue(attributes.accept(resolved), "Un-matched type");
        this.settings.put(attributes, resolved);
        return this;
    }

    /**
     * Apply settings.
     */
    protected void applyValues(Component<T, S> component) {
        settings.forEach((k, v) -> k.apply(component, v));
    }

}
